package cn.fl.ref.fncprjfstpyinfo.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.crbaseinfo.transform.CrBaseInfoVOTransform;
import cn.fl.ref.crbaseinfo.vo.CrBaseInfoVO;
import cn.fl.ref.fnccappayeeinfo.service.IFncCapPayeeInfoService;
import cn.fl.ref.fncprjbscinfo.service.IFncPrjBscInfoService;
import cn.fl.ref.fncprjbscinfo.vo.FncPrjBscInfoVO;
import cn.fl.ref.fncprjfstpyinfo.service.IFncPrjFstpyInfoService;
import cn.fl.ref.fncprjfstpyinfo.transform.FncPrjDetailVOTransform;
import cn.fl.ref.fncprjfstpyinfo.vo.FncPrjDetailVO;
import cn.fl.ref.fncprjfstpyinfo.vo.FncPrjFstpyInfoVO;
import cn.fl.ref.fribaseinfo.service.IFriBaseInfoService;
import cn.fl.ref.fribaseinfo.transform.BankAccInfoVOTransform;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.ref.scmbaseinfo.service.IScmBaseInfoService;
import cn.fl.ref.scmbaseinfo.vo.ScmBaseInfoVO;
import cn.fl.system.condition.SysBkAccInfoQueryCondition;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.vo.SysBkAccInfoVO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import fl.ref.crbaseinfo.condition.CrBaseInfoQueryCondition;
import fl.ref.crbaseinfo.dto.CrBaseInfoDTO;
import fl.ref.crbaseinfo.service.ICrBaseInfoApiService;
import fl.ref.fnccappayeeinfo.service.IFncCapPayeeInfoApiService;
import fl.ref.fncprjbscinfo.condition.FncPrjBscInfoQueryCondition;
import fl.ref.fncprjfstpyinfo.condition.FncPrjFstpyInfoQueryCondition;
import fl.ref.fncprjfstpyinfo.constant.FncPrjFstpyInfoConstant;
import fl.ref.fncprjfstpyinfo.dto.FncPrjDetailDTO;
import fl.ref.fribaseinfo.condition.BankAccInfoQueryCondition;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.dto.BankAccInfoDTO;
import fl.ref.fribaseinfo.service.IBankAccInfoApiService;
import fl.ref.scmbaseinfo.condition.ScmBaseInfoQueryCondition;
import fl.ref.scmbaseinfo.constant.ScmInfoConstant;


/**
 * @Description: 融资项目首付款信息
 */
@Controller
@RequestMapping("/rfbase/fncprjfstpyinfo")
public class FncPrjFstpyInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FncPrjFstpyInfoController.class);

	@Autowired
	private IFncPrjFstpyInfoService fncPrjFstpyInfoService;

	@Autowired
	private IFriBaseInfoService friBaseInfoService;
	
	@Autowired
	private IFncCapPayeeInfoService fncCapPayeeInfoService;

	@Autowired
	private IScmBaseInfoService scmBaseInfoService;
	
	@Autowired
	private ICrBaseInfoApiService crBaseInfoApiService;
	@Autowired
	private IFncPrjBscInfoService fncPrjBscInfoService;
	
	/**
	 * 融资机构银行帐号信息
	 */
	@Autowired
	private IBankAccInfoApiService bankAccInfoApiService;
	
	/**
	 * 狮桥支付银行账户
	 */
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET })
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		logger.info("加载融资项目首付款信息列表页面.");
		ModelAndView view = new ModelAndView("/rfbase/fncprjfstpyinfo/fncPrjFstpyInfoList");
		// 融资机构
		FriBaseInfoQueryCondition condition = new FriBaseInfoQueryCondition();
		condition.setInsUseCd("1");
		List<FriBaseInfoVO> friBaseInfoVOs = friBaseInfoService.searchList(condition);
		List<KeyValueVO> fundRasInsNmList = fncPrjFstpyInfoService.transformToKeyValue(friBaseInfoVOs, FriBaseInfoVO.class.getName(), "id",
				"fundRasInsNm");
		view.addObject("fundRasInsNmList", fundRasInsNmList);
		// 融资方案
		List<ScmBaseInfoVO> scmBaseInfoVOs = scmBaseInfoService.selectBscLstByCondition(new ScmBaseInfoQueryCondition());
		List<KeyValueVO> fncScmNmList = fncPrjFstpyInfoService.transformToKeyValue(scmBaseInfoVOs, ScmBaseInfoVO.class.getName(), "fncScmId",
				"fncScmNm");
		view.addObject("fncScmNmList", fncScmNmList);
		// 是否
		List<KeyValueVO> wthrDdctFstPayList = fncPrjFstpyInfoService.searchWthrKVList();
		view.addObject("wthrDdctFstPayList", wthrDdctFstPayList);
		view.addObject("frilist",JSON.toJSONString(friBaseInfoVOs));
		return view;
	}

	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("/rfbase/fncprjfstpyinfo/fncPrjFstpyInfoAdd");
		String fncPrjId = request.getParameter("fncPrjId");
		view.addObject("fncPrjId", fncPrjId);
		FncPrjDetailVO fncPrjDetailVO = this.fncPrjFstpyInfoService.selectByFncPrjId(Long.valueOf(fncPrjId));
		if(fncPrjDetailVO != null) {
			view.addObject("fncInsId", fncPrjDetailVO.getFncInsId());
			view.addObject("wthrFstPay", fncPrjDetailVO.getWthrFstPay());
			
			//保证金坐扣状态：默认已付，则需查询状态为不作废的再融资项目
			FncPrjBscInfoQueryCondition qc=new FncPrjBscInfoQueryCondition();
			qc.setIsdel(BaseConstant.IS_YESNO_NO);
			qc.setNotEqualFncprjstcd(14);//作废状态
			view.addObject("fncPrjBscInfoVOList", fncPrjBscInfoService.searchList(qc));
		}
		
		if("FLOW_0000_R014_001_002".equals(request.getParameter("taskDefKey"))) {
			view.addObject("mgrUpdateNode",true);//来自再融资首付款流程-融资经理修改节点
		}
		
		return view;
	}

	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("rfbase/fncprjfstpyinfo/fncPrjFstpyInfoDetail");
		String fncPrjId = request.getParameter("fncPrjId");
		view.addObject("fncPrjId", fncPrjId);
		return view;
	}
	
	/**
	 * 加载金融机构收款户选择框
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/initRecvList.do" ,method = {RequestMethod.GET})
	public @ResponseBody  List<BankAccInfoDTO>  initRecvList(HttpServletRequest request, HttpServletResponse response) {
		String instid = request.getParameter("insId");
		String banAccNm = request.getParameter("banAccNm");
		String accNum = request.getParameter("accNum");
		if (StringUtils.isEmpty(instid)) {
			return null;
		}
		BankAccInfoQueryCondition bankAccInfoQC = new BankAccInfoQueryCondition();
		bankAccInfoQC.setFundRasInsId(Long.parseLong(instid));
		bankAccInfoQC.setBanAccNm(banAccNm);
		bankAccInfoQC.setAccNum(accNum);
		bankAccInfoQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
		bankAccInfoQC.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<BankAccInfoDTO> banklst = bankAccInfoApiService.searchList(bankAccInfoQC);
		
		return banklst;
	}
	
	/**
	 * 加载狮桥支付账户选择框
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/initPayList.do" ,method = {RequestMethod.GET})
	public @ResponseBody  List<SysBkAccInfoVO>  initPayList(HttpServletRequest request, HttpServletResponse response) {
		SysBkAccInfoQueryCondition qc = new SysBkAccInfoQueryCondition();
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<SysBkAccInfoVO> bkAccList = sysBkAccInfoService.searchList(qc);
		return bkAccList;
	}
	

	/**
	 * 初始化列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String fncInsId = request.getParameter("fncInsId");
		String fncScmId = request.getParameter("fncScmId");
		String fncPrjNm = request.getParameter("fncPrjNm");
		String wthrDdctFstPay = request.getParameter("wthrDdctFstPay");
		String fncFstpyStcd = request.getParameter("fncFstpyStcd");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		FncPrjFstpyInfoQueryCondition qc = new FncPrjFstpyInfoQueryCondition();

		if (StringUtils.isNotEmpty(fncInsId)) {
			qc.setFncInsId(fncInsId);
		}
		if (StringUtils.isNotEmpty(fncScmId)) {
			qc.setFncScmId(fncScmId);
		}
		if (StringUtils.isNotEmpty(fncPrjNm)) {
			qc.setFncPrjNm(fncPrjNm);
		}
		if(StringUtils.isNotEmpty(wthrDdctFstPay)){//是否坐扣 0：否 1:是  2：默认已付
			List<Integer>  wthrDdctFstPayList = new ArrayList<Integer>();
			
			String[] wthrDdctFstPayArray = wthrDdctFstPay.split(",");
			for (String wthrDdctFstPayItem : wthrDdctFstPayArray) {
				wthrDdctFstPayList.add(Integer.valueOf(wthrDdctFstPayItem));
			}
			qc.setWthrDdctFstPayList(wthrDdctFstPayList);
		}
		if (StringUtils.isNotEmpty(fncFstpyStcd)) {
			qc.setFncFstpyStcd(fncFstpyStcd);
		}
		page.setCondition(qc);

		Pager pager = this.fncPrjFstpyInfoService.selectFncPrjDetailListPage(page);
		List<FncPrjDetailDTO> list = (List<FncPrjDetailDTO>) pager.getResultList();
		List<FncPrjDetailVO> voList = FncPrjDetailVOTransform.toVOList(list);
		ListPageVO<FncPrjDetailVO> listPage = new ListPageVO<FncPrjDetailVO>(voList, pager.getTotalCount());

		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fncPrjId = request.getParameter("fncPrjId");
		String acFiAmt = request.getParameter("acFiAmt");

		String totalPay = request.getParameter("totalPay");
		String fstpyAccno = request.getParameter("fstpyAccno");
		String fstpyPayAccno = request.getParameter("fstpyPayAccno");
		String fstpyArray = request.getParameter("fstpyArray"); // 银行帐号信息
		
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		try {

			// 验证
			FncPrjDetailVO entity = new FncPrjDetailVO();
			entity.setFncPrjId(Long.valueOf(fncPrjId));
			if (StringUtils.isNotEmpty(acFiAmt)) {
				entity.setAcFiAmt(new BigDecimal(acFiAmt));
			}
			entity.setUpdPsnEcdd(CurrentThreadContext.getCurrentUserId().toString());
			entity.setUpdTm(new Date());

			entity.setFstpyAccno(fstpyAccno);
			entity.setFstpyPayAccno(fstpyPayAccno);
			// 最终付款金额为实际付款金额合计+预付利息
			// entity.setFstpyAmt(new BigDecimal(totalPay).add(entity.getExpInt()));
			// TODO 最终付款金额为实际付款金额合计
			entity.setFstpyAmt(new BigDecimal(totalPay));

			// 首付款信息
			JSONArray fstpyJsonArray = JSONArray.parseArray(fstpyArray);
			List<FncPrjFstpyInfoVO> fncPrjFstpyInfoVOs = new ArrayList<>();
			if (fstpyJsonArray.size() > 0) {
				for (int i = 0; i < fstpyJsonArray.size(); i++) {
					FncPrjFstpyInfoVO fncPrjFstpyInfoVO = JSONObject.toJavaObject(fstpyJsonArray.getJSONObject(i), FncPrjFstpyInfoVO.class);
					fncPrjFstpyInfoVOs.add(fncPrjFstpyInfoVO);
				}
			}
			entity.setFncPrjFstpyInfoVOs(fncPrjFstpyInfoVOs);

			this.fncPrjFstpyInfoService.modify(entity);
//			this.fncCapPayeeInfoService.addCapFirstpay(Long.parseLong(fncPrjId));

			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("更新成功!");
			BaseLogUtils.info(logger, "modify", "修改数据,id[" + fncPrjId + "].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			ex.printStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改数据失败,id[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			ex.printStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误");
			BaseLogUtils.error(logger, "modify", "修改数据失败,id[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 得到一条记录
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String fncPrjId = request.getParameter("fncPrjId");
		String wthrDdctFstPay = request.getParameter("wthrDdctFstPay");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		FncPrjDetailVO fncPrjDetailVO = null;
		try {
			//1 查询项目的方案、方案明细、机构等
			fncPrjDetailVO = this.fncPrjFstpyInfoService.selectByFncPrjId(Long.valueOf(fncPrjId));
			// 设置数据字典项
			String sysCd = CurrentThreadContext.getCurrentSysCd();
			String orgCd = CurrentThreadContext.getCurrentOrgCd();
			Map<String, String> fncCrdnltCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, ScmInfoConstant.FNC_SCM_BSC_INFO_FNC_CRDNLT);// 融资基数
			if (null != fncPrjDetailVO.getFncCrdnlt()) {	
				fncPrjDetailVO.setFncCrdnltNm(fncCrdnltCdMap.get(fncPrjDetailVO.getFncCrdnlt().toString()));
			}
			
			FncPrjFstpyInfoQueryCondition qc = new FncPrjFstpyInfoQueryCondition();
			qc.setFncPrjId(fncPrjId);
			if(StringUtils.isNotEmpty(wthrDdctFstPay)){//是否坐扣 0：否 1:是  2：默认已付
				List<Integer>  wthrDdctFstPayList = new ArrayList<Integer>();
				
				String[] wthrDdctFstPayArray = wthrDdctFstPay.split(",");
				for (String wthrDdctFstPayItem : wthrDdctFstPayArray) {
					wthrDdctFstPayList.add(Integer.valueOf(wthrDdctFstPayItem));
				}
				qc.setWthrDdctFstPayList(wthrDdctFstPayList);
			}
			
			//2 查询首付明细列表（可能包括多条保证金）
			List<FncPrjFstpyInfoVO> fncPrjFstpyInfoVOs = this.fncPrjFstpyInfoService.searchList(qc);
			/*// 保证金计入首付款
			if (fncPrjDetailVO.getMrgnWthrInPay() == 1 && !fncPrjDetailVO.getMrgnPct().equals(new BigDecimal(0))) {
				qc.setScmChrgId("0"); // 保证金
				FncPrjFstpyInfoVO mrgnFstpyInfoVO = this.fncPrjFstpyInfoService.selectByScmChrgId(qc);
				if (null == mrgnFstpyInfoVO) {
					mrgnFstpyInfoVO = new FncPrjFstpyInfoVO();
				}
				mrgnFstpyInfoVO.setScmChrgId("0");
				mrgnFstpyInfoVO.setCostNm("保证金");
				mrgnFstpyInfoVO.setCostPct(fncPrjDetailVO.getMrgnPct());
				mrgnFstpyInfoVO.setWthrDdct(fncPrjDetailVO.getWthrFstPay()); // 保证金是否坐扣
				
				if("confirmAccount".equals(from)) {//来自：确认到账
					// 保证金坐扣状态：不坐扣0,默认已付2
					 if(fncPrjDetailVO.getWthrFstPay() != null &&  fncPrjDetailVO.getWthrFstPay() == 1 ) {
						fncPrjFstpyInfoVOs.add(0, mrgnFstpyInfoVO);
					}
					 
				}else {//首付款
					// 保证金坐扣状态：不坐扣0,默认已付2
					 if(fncPrjDetailVO.getWthrFstPay() != null && 
							(fncPrjDetailVO.getWthrFstPay() == 0 || fncPrjDetailVO.getWthrFstPay() == 2)) {
						fncPrjFstpyInfoVOs.add(0, mrgnFstpyInfoVO);
					}
				}
				
				 
				// 坐扣或不坐扣 
				if (StringUtils.isEmpty(wthrDdctFstPay)) {
					if(fncPrjFstpyInfoVOs != null && !fncPrjFstpyInfoVOs.contains(mrgnFstpyInfoVO)) {
						fncPrjFstpyInfoVOs.add(0, mrgnFstpyInfoVO);
					}
				}
			}*/
			
			//设置收款账户、付款账户信息、关联保证金的项目
			if(fncPrjFstpyInfoVOs != null && fncPrjFstpyInfoVOs.size() >0) {
				for (FncPrjFstpyInfoVO fncPrjFstpyInfoVO : fncPrjFstpyInfoVOs) {
					if (StringUtils.isNotEmpty(fncPrjFstpyInfoVO.getRecAccount())) {//收款账户信息
						BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(Long.valueOf(fncPrjFstpyInfoVO.getRecAccount()), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
						fncPrjFstpyInfoVO.setBankAccInfoVO(BankAccInfoVOTransform.toVO(bankAccInfoDTO));
					}
					if (StringUtils.isNotEmpty(fncPrjFstpyInfoVO.getPayAccount())) {//付款账户信息
						SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(Long.valueOf(fncPrjFstpyInfoVO.getPayAccount()));
						fncPrjFstpyInfoVO.setSysBkAccInfoVO(bkAccInfoVO);
					}
					if (StringUtils.isNotEmpty(fncPrjFstpyInfoVO.getWhichPrjId())) {//查询关联保证金的项目
						fncPrjFstpyInfoVO.setFncPrjBscInfoVO(fncPrjBscInfoService.selectById(Long.valueOf(fncPrjFstpyInfoVO.getWhichPrjId())));
					}
				}
			}
			
			fncPrjDetailVO.setFncPrjFstpyInfoVOs(fncPrjFstpyInfoVOs);
			
			// 授信余额
			CrBaseInfoQueryCondition condition=new CrBaseInfoQueryCondition();
			condition.setCrNmQ(fncPrjDetailVO.getCrNm());
			List<CrBaseInfoDTO> listDTO = crBaseInfoApiService.searchList(condition);
			if (CollectionUtils.isNotEmpty(listDTO)) {
				CrBaseInfoVO crBaseInfoVO = CrBaseInfoVOTransform.toVO(listDTO.get(0));
				fncPrjDetailVO.setCrBal(crBaseInfoVO.getCrBal());
			}
			
			/*// 保证金收款帐户信息
			if (StringUtils.isNotEmpty(fncPrjDetailVO.getFstpyAccno())) {
				BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(Long.valueOf(fncPrjDetailVO.getFstpyAccno()), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				fncPrjDetailVO.setBaccInfoVO(BankAccInfoVOTransform.toVO(bankAccInfoDTO));
			}
			
			// 我司付款帐户信息
			if (StringUtils.isNotEmpty(fncPrjDetailVO.getFstpyPayAccno())) {
				SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(Long.valueOf(fncPrjDetailVO.getFstpyPayAccno()));
				fncPrjDetailVO.setBkAccInfoVO(bkAccInfoVO);
			}*/
			
			result.setData(fncPrjDetailVO);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取数据失败,主键[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getById", "获取数据失败,主键[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 无首期费用 已坐扣
	 */
	@RequestMapping(value = "/noFirstPay.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object noFirstPay(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fncPrjId = request.getParameter("fncPrjId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "设置已坐扣成功!");
		FncPrjDetailVO fncPrjDetailVO = null;
		try {
			fncPrjDetailVO = this.fncPrjFstpyInfoService.selectByFncPrjId(Long.valueOf(fncPrjId));
			// 坐扣
			if(fncPrjDetailVO.getWthrDdctFstPay() == 1){
				// 项目状态已付款
				fncPrjDetailVO.setFncPrjStcd(FncPrjFstpyInfoConstant.FNC_PRJ_BSC_INFO_ST_CD_FIRST_PAY_PASS);
				fncPrjDetailVO.setFstpyTm(new Date());
			}
			// 坐扣/不坐扣
			if (fncPrjDetailVO.getWthrDdctFstPay() == 2) {
				if (fncPrjDetailVO.getFstpySt() == 0) {
					// 首付款操作状态为“已坐扣”
					fncPrjDetailVO.setFstpySt(FncPrjFstpyInfoConstant.FSTPY_ST_WTHR);
				}else {
					// 项目状态已付款
					fncPrjDetailVO.setFncPrjStcd(FncPrjFstpyInfoConstant.FNC_PRJ_BSC_INFO_ST_CD_FIRST_PAY_PASS);
					fncPrjDetailVO.setFstpyTm(new Date());
				}
			}
			this.fncPrjFstpyInfoService.modifyFncPrjDetail(fncPrjDetailVO);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "noFirstPay", "修改无首期费用失败,主键[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "noFirstPay", "修改无首期费用失败,主键[" + fncPrjId + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 审核流程
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addFlowStart.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addFlowStart(HttpServletRequest request, HttpServletResponse response) {

		String fncPrjId = request.getParameter("fncPrjId");
		String procDefKey = request.getParameter("procDefKey");
		String procDefId = request.getParameter("procDefId");
		String procNo = request.getParameter("procNo");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(fncPrjId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "addFlowStart", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
			return result;
		}
		FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
		try {
			// 流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
			flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
			flowStartParamsDTO.setWordsInTheTable(FncPrjFstpyInfoConstant.REF_FLOW_S_H_FST);
			flowStartParamsDTO.setProcDefKey(procDefKey);
			flowStartParamsDTO.setProcDefId(procDefId);
			flowStartParamsDTO.setProcNo(procNo);
			flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			flowStartParamsDTO.setBusinessKey(fncPrjId);
			flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
			flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
			flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
			flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);// 优先级,可以在界面定义
			flowStartParamsDTO.setBusinessType(FncPrjFstpyInfoConstant.FLOW_BUSINESS_TYP_FNC_PRJ_BSC_INFO);// 确认用不用
			flowStartParamsDTO.setModelTyp(FncPrjFstpyInfoConstant.FLOW_MODEL_TYP_FST);// 需要和流程配置中的模块类型匹配
			flowStartParamsDTO.setSearchDesc(CurrentThreadContext.getCurrentOrgDispNm() + "、首付款流程大字段参数未定!");
			DataResultDTO dataResultDTO = this.fncPrjFstpyInfoService.addFlowStart(flowStartParamsDTO);
			if (!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}
			if (null != dataResultDTO && null != dataResultDTO.getData()) {
				FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
				if (StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())) {
					result.setInfo("下一个节点操作人:" + flowStartedDTO.getNextOperUsrNames());
				} else {
					result.setInfo("流程发起成功");
				}
			}
			/*
			 * if(null != dataResultDTO.getInfo() && StringUtils.isNotEmpty(dataResultDTO.getInfo())){ StringBuffer info = new StringBuffer();
			 * info.append(result.getInfo()); info.append(SysLogConstant.LOG_FLOW_NEXT_TASK_OPERATOR); info.append(dataResultDTO.getInfo());//操作人
			 * result.setInfo(info.toString()); }
			 */
//			this.fncCapPayeeInfoService.addCapFirstpay(Long.parseLong(fncPrjId));
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "addFlowStart",
					"错误[BaseException],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addFlowStart",
					"错误[Exception],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
			e.getStackTrace();
		}
		return result;
	}
}
